home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue62 / WStrings / wideprop.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-08-15  |  3.3 KB  |  103 lines

  1. unit WideProp;
  2.  
  3. interface
  4.  
  5. // Support for WideString properties.
  6. //
  7. // Supporting a property of type WideString is a nontrivial task.
  8. // The Object Inspector supports only ANSI strings, so you need
  9. // a custom property editor. The property editor can pop up a dialog
  10. // box where the user can enter a wide string.
  11. //
  12. // The TypInfo unit always maps wide strings to ANSI strings,
  13. // so this unit defines routines for getting and setting a
  14. // property value in its native WideString format. This turns out
  15. // to be more difficult than it should be, and requires detailed
  16. // knowledge of the format of a property infomation record.
  17. //
  18. // When declaring a component with a WideString-type property,
  19. // make sure to declare it with the Stored directive set to False,
  20. // and override DefineProperties to store the property value as a
  21. // WideString (call TReader.ReadWideString and TWriter.WriteWideString).
  22. // Otherwise, Delphi converts the wide string to an ANSI string
  23. // to store in the DFM.
  24. //
  25. // Copyright ⌐ 2000 Tempest Software, Inc.
  26.  
  27. uses Windows, Messages, SysUtils, Classes, Graphics, DsgnIntf;
  28.  
  29. // The object inspector does not support wide strings, so the user must
  30. // edit and view the wide string property value in a separate dialog box.
  31. // The TWideStringProperty property editor manages the dialog box.
  32. type
  33.   TWideStringProperty = class(TStringProperty)
  34.   protected
  35.     function GetWideStrValue: WideString;
  36.     function GetWideStrValueAt(Index: Integer): WideString;
  37.     procedure SetWideStrValue(const Value: WideString);
  38.   public
  39.     procedure Edit; override;
  40.     function GetAttributes: TPropertyAttributes; override;
  41.     procedure PropDrawValue(Canvas: TCanvas; const Rect: TRect; Selected: Boolean); override;
  42.   end;
  43.  
  44. implementation
  45.  
  46. uses Consts, Controls, Forms, Dialogs, StdCtrls, WideInfo, WideQuery;
  47.  
  48. { TWideStringProperty }
  49.  
  50. procedure TWideStringProperty.Edit;
  51. resourcestring
  52.   sCaption = 'Set Property Value';
  53. var
  54.   Text: WideString;
  55. begin
  56.   Text := GetWideStrValue;
  57.   if InputQueryW(sCaption, GetName + ':', Text) then
  58.     SetWideStrValue(Text);
  59. end;
  60.  
  61. // Add the paDialog attribute so the user can set the property
  62. // value in the Wide String dialog box.
  63. function TWideStringProperty.GetAttributes: TPropertyAttributes;
  64. begin
  65.   Result := inherited GetAttributes + [paDialog, paReadOnly];
  66. end;
  67.  
  68. // Get the property value as a wide string.
  69. function TWideStringProperty.GetWideStrValue: WideString;
  70. begin
  71.   Result := GetWideStrValueAt(0);
  72. end;
  73.  
  74. function TWideStringProperty.GetWideStrValueAt(Index: Integer): WideString;
  75. begin
  76.   Result := GetWideStrProp(GetComponent(Index), GetPropInfo);
  77. end;
  78.  
  79. procedure TWideStringProperty.PropDrawValue(Canvas: TCanvas;
  80.   const Rect: TRect; Selected: Boolean);
  81. var
  82.   Value: WideString;
  83. begin
  84.   Value := GetWideStrValue;
  85.   // Need to ensure a Unicode font.
  86.   Canvas.Font.Name := 'Arial';
  87.   Win32Check(ExtTextOutW(Canvas.Handle, Rect.Left, Rect.Top,
  88.                          Eto_Clipped or Eto_Opaque, @Rect,
  89.                          PWideChar(Value), Length(Value), nil));
  90. end;
  91.  
  92. // Set the property value as a wide string.
  93. procedure TWideStringProperty.SetWideStrValue(const Value: WideString);
  94. var
  95.   I: Integer;
  96. begin
  97.   for I := 0 to PropCount-1 do
  98.     SetWideStrProp(GetComponent(I), GetPropInfo, Value);
  99.   Modified;
  100. end;
  101.  
  102. end.
  103.